namespace('org.mediaconverter');

org.mediaconverter.wizard = Class.extend({
	
	/**
	 * Constructor
	 */
	init: function(options) {
		
		this.options = options || {};
		this.$wizard = $('#wizard');
		this.$content = $('div.content', this.$wizard);
		this.jobListReloadInterval = null;
		
		this.initEvents();
		
		//load the job list
		this.loadJobList();
		
		//load home screen
		this.loadStep(this.options['homeUrl']);
		
	},
	
	
	/**
	 * Init Events
	 */
	initEvents: function() {
		
		//links which have to be loaded in wizard content
		this.$content.on('click', 'a.loadInWizardContent', $.proxy(function(ev) {
			ev.preventDefault();
			this.loadStep($(ev.currentTarget).attr('href'));
		}, this));
		
		//delete job listener
		this.$wizard.on('click', 'div.queue div.files a.remove', $.proxy(function(ev) {
			ev.preventDefault();
			$.get($(ev.currentTarget).attr('href'), $.proxy(function(res) {
				if(res == 'ok') {
					this.loadJobList($.proxy(function(res) {
						//remove "next step" icon if there are no files 
						if($(res).filter('div.no_files').length) {
							$('a.icon.next', this.$content).fadeOut(150);
						}
					}, this));
				}
			}, this));
		}, this));
		
	},
	
	/**
	 * Load step
	 * @param	url		URL to the view
	 */
	loadStep: function(url) {
		
		this.$content.animate({
			'opacity': 0
		}, 150, $.proxy(function() {
			
			//there is some recursion in the following lines to make sure 
			//everything gets reinitalized after content was reloaded via ajax
			var ajaxOptions = {
				url: url,
				cache: false,
				success: $.proxy(function(res) {
					
					//replace content
					this.$content.html(res);
					this.$content.animate({
						'opacity': 1
					}, 150);
					
					//loading screen
					var $loadingScreen = $('<div class="loading"><div class="status"></div></div>') 
					
					//init ajax form
					var formOptions = {
						beforeSubmit: $.proxy(function(data, $form, options){
							
							//check if there is a upload_progress_url attribute
							var upload_progress_url = null;
							for(var i = 0; i < data.length; i++) {
								if(data[i]['name'] == 'cancel') {
									//do nothing if the cancel button was pressed
									return;
								} else if(data[i]['name'] == 'upload_progress_url') {
									upload_progress_url = data[i]['value'];
								}
							}
							if(upload_progress_url) {
								//display the loading screen
								this.$content.append($loadingScreen);
								$loadingScreen.fadeIn(150);
								
								//load the progress screen
								$.ajax({
									url: upload_progress_url,
									success: $.proxy(function(res) {
										$('div.status', $loadingScreen).html(res);
									}, this)
								})
							}
							
						}, this),
						success: $.proxy(function(res) {
							
							//remove loading screen
							$loadingScreen.fadeOut(150, function() {
								$(this).remove();
							})
							
							//if the same screen gets displayed we do not have to display a transition
							if($(res).filter('div.enteralink, div.uploadafile, div.browseyoutube, div.selectprofile').length) {
								this.$content.html(res);
								$('form', this.$content).ajaxForm(formOptions);
								
							//execute transition
							} else {
								this.$content.animate({
									'opacity': 0
								}, 150, $.proxy(function() {
									ajaxOptions['success'](res);
									this.loadJobList();
								}, this))
							}
						}, this)
					}
					$('form', this.$content).ajaxForm(formOptions);
					
					this.reloadAdvertisement();
					
				}, this)
			}
			$.ajax(ajaxOptions)
			
		}, this));
		
	},
	

	/**
	 * Reload job list
	 * @param	callback	function that will be executed after reload
	 */
	loadJobList: function(callback) {
		
		$.ajax({
			url: this.options['jobsUrl'],
			cache: false,
			success: $.proxy(function(res) {
				$('div.queue div.files', this.$wizard).html(res);
				if(callback) {
					callback(res);
				}
			}, this)
		})
		
	},
	
	
	/**
	 * Auto reload job list
	 */
	autoReloadJobList: function() {
		
		//refreshs the job list every 5 second
		this.jobListReloadInterval = window.setInterval($.proxy(function() {
			this.loadJobList();
		}, this), 5000);
		
	},
	
	
	/**
	 * Stop reloading job list
	 */
	stopReloadingJobList: function() {
		
		window.clearInterval(this.jobListReloadInterval);
		
	},
	
	
	/**
	 * Reload the advertisement in the conversion wizard
	 */
	reloadAdvertisement: function() {
		
		//reload advertisement
		try {
			$('div.advertisement iframe', this.$wizard).get(0).contentDocument.location.reload(true);
		} catch(e) {};
		
	}
	
	
});